using Godot;
using System;

using CurrentGame.NPC;
using CurrentGame.GUI.PlaceGrid;
using CurrentGame.GUI;
using CurrentGame.Placements;

namespace CurrentGame.Levels.StartLevel;

public partial class StartLevelMain : Node
{
    [Export]
    public Godot.Collections.Array<ShadeCharacter> ReadyShades { get; set; } = [];
    [Export]
    public StaticBody2D InitialWall { get; set; }
    [Export]
    public AnimationPlayer Animation {  get; set; }
    [Export]
    public Timer TextTimer { get; set; }
    [Export]
    public PlacableTexts SceneText { get; set; }
    [Export]
    public PlacableTexts SceneText2 { get; set; }

    [Export]
    public LevelTeleport Teleport { get; set; }
    [Export]
    public string HardNextLevel { get; set; }
    [Export]
    public string EZNextLevel { get; set; }

    private SceneSwitch _sceneSwitch;
    private PlaceModeGrid _placeGrid;
    private Player _player;
    private EnterGameUi _enterGameUi;
    private PauseMenu _pauseMenu;
    private CinematicCamera _cinematicCamera;
    private GameProgressManager _progressManager;

    private int _talkState = 0;

    private bool _hardMode = false;

    public override void _Ready()
    {
        var map = GetNode<NodeReferenceMap>(Constants.NodeRefMapPath);
        _sceneSwitch = map.SceneSwitch;
        _sceneSwitch.SceneStart += Signal_SceneStart;
        _player = map.MainPlayer;
        _enterGameUi = map.EntryUi;
        _pauseMenu = map.PauseMenu;
        _placeGrid = map.PlacementModeGrid;
        _cinematicCamera = map.GameCamera;
        _hardMode = map.ProgressManager.HardMode;
        _progressManager = map.ProgressManager;

        _player.AttackEnabled = false;
        _player.PlaceModeAvailable = false;
        if (_progressManager.StartDeathCount == 2)
        {
            _progressManager.HardMode = true;
            SceneText.Text = "你为何这样做呢？";
            SceneText2.Text = "困难模式已开启。";
            _player.SetHealthPoint(2);
        }
        else if (_progressManager.StartDeathCount >= 3)
        {
            _progressManager.HardMode = true;
            _progressManager.ExtremeMode = true;
            SceneText.Text = "...如你所愿。";
            SceneText2.Text = "极限模式已开启。";
            _player.SetHealthPoint(1);
        }
        else
        {
            _player.SetHealthPoint(3);
        }
        _pauseMenu.EnablePauseMenu = false;
        _enterGameUi.EnterGameUIStateChanged += Signal_EnterGameUIStateChanged;
        _player.HealthPointChanged += Signal_PlayerHealthChanged;


        Teleport.NextSceneName = _hardMode ? HardNextLevel : EZNextLevel;
    }


    public override void _ExitTree()
    {
        _player.HealthPointChanged -= Signal_PlayerHealthChanged;
        _sceneSwitch.SceneStart -= Signal_SceneStart;
        _enterGameUi.EnterGameUIStateChanged -= Signal_EnterGameUIStateChanged;
    }




    private void Signal_EnterGameUIStateChanged(bool show)
    {
        // Key Pressed
        if (!show)
        {
            _pauseMenu.EnablePauseMenu = true;
            Animation.Play("light_remove");
        }
    }

    private void Signal_AnimationFinished(StringName animName)
    {
        InitialWall.QueueFree();
        _player.EnableInputCheck();
        TextTimer.WaitTime = 3.0f;
        TextTimer.Start();
    }

    private void Signal_Timeout()
    {
        if (_talkState == 0)
        {
            SceneText.ShowText = true;
            TextTimer.WaitTime = 3.0f;
            TextTimer.Start();
            _talkState = 10;
        }
        else if (_talkState == 10)
        {
            SceneText2.ShowText = true;
            _talkState = 20;
        }
    }

    private void Signal_SceneStart()
    {
        _player.SkipInputCheck();
        foreach (var shade in ReadyShades)
        {
            shade.ActivateFallDetection();
            _placeGrid.AppendReadyShade(shade);
        }
        _enterGameUi.ShowEnterUI();
    }

    private void Signal_PlayerHealthChanged(int hp)
    {
        if (hp <= 0)
        {
            _progressManager.StartDeathCount += 1;
        }
    }
}
